//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import cardPopup from "./comm/cardPopup.vue"
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {cardPopup},
  data() {
  return {
    userForm:{
      type:'1',
      id_card_type:'',
      id_card_no:'',
      id_card_valid_type:'',
      id_card_valid_start_time:'',
      id_card_valid_end_time:'',
      name:'',
      mobile:'',
    },
    cardTypeName:'',
    cardValidName:'',
    cardTypeIndex:0,
    cardValidIndex:0,
    cardIndex:0,
    cardTypeShow:false,
    dateShow:false,
    cardTitle:'',
    cardType:'',
    cardTimeType:'',
    cardArr:[],
    succeedRadio:'1',
    cardTypeArr:[
      {
        name:'身份证',
        value:17
      },
      {
        name:'护照',
        value:18
      },
      {
        name:'港澳居民来往内地通行证',
        value:19
      },
      {
        name:'台湾居民来往内地通行证',
        value:20
      }
    ],
    cardValidArr:[
      {
        name:'长期有效',
        value:1
      },
      {
        name:'非长期有效',
        value:0
      }
    ],
    minDate: new Date(1970, 0, 1),
    maxDate: new Date(2050, 12, 1),
    currentDate: new Date(),
    startTime:'',
    endTime:'',
    withdrawInfo:{},
    //修改还是提交的状态码
    btnStatus:0,//0就是未提交，1是开户成功，2是开户成功修改中
    iPnoneBottomBol: false, //是否是苹果手机
    
  };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    initData() {
      this.userForm.type = '1';
      this.userForm.id_card_type = '';
      this.userForm.id_card_no = '';
      this.userForm.id_card_valid_type = '';
      this.userForm.id_card_valid_start_time = '';
      this.userForm.id_card_valid_end_time = '';
    },
    getiPnoneBottom() { //检测是否为苹果手机
      var userAgent = navigator.userAgent;
      var isiOS = !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
      isiOS?this.iPnoneBottomBol = true:this.iPnoneBottomBol = false;
    },
    formatter(type, val) {
      if (type === 'year') {
        return val + '年';
      }
      if (type === 'month') {
        return val + '月';
      }
      if (type === 'day') {
        return val + '日';
      }
      return val;
    },
    cardTypeOn(name) {
      if(this.btnStatus == 1) return false;
      if(name == 'type') {
        this.cardTitle = '个人证件类型';
        this.cardArr = this.cardTypeArr;
        this.cardIndex = this.cardTypeIndex;
      } else {
        this.cardTitle = '证件有效期类型';
        this.cardArr = this.cardValidArr;
        this.cardIndex = this.cardValidIndex;
      }
      this.cardType = name;
      if((this.btnStatus == 2 && name != 'type') || this.btnStatus == 0) {
        this.cardTypeShow = true;
      }
    },
    
    cardConfrim(data) {
      if( this.cardType == 'type') {
        this.userForm.id_card_type = this.cardTypeArr[data.cardIndex].value;
        this.cardTypeName = this.cardTypeArr[data.cardIndex].name;
        this.cardTypeIndex= data.cardIndex;
      } else {
        this.userForm.id_card_valid_type = this.cardValidArr[data.cardIndex].value;
        this.cardValidName =  this.cardValidArr[data.cardIndex].name;
        this.cardValidIndex= data.cardIndex;
      }
    },
    cardStartTime(name) {
      if(this.btnStatus == 1) return false;
      this.cardTimeType = name;
      this.dateShow = true;
    },
    windAccountTo() {
      this.$router.push(this.fun.getUrl("windAccount", {}));
    },
    windEdit() {
      this.btnStatus = 2;
    },
    accountSubmit() {
      if((this.startTime >= this.endTime) && ( this.userForm.id_card_valid_type != 1  || this.userForm.id_card_type != 17)) {
        this.$toast('个人证件有效期开始日期不能大于截至日期');
        return false;
      }
      if(this.withdrawInfo.id && this.btnStatus == 2) {
        this.editAccount();
      } else {
        $http.post("plugin.integration-pay-share.frontend.withdraw.open-account", {form:this.userForm}, "加载中..").then(res => {
          if (res.result == 1) {
            let data = res.data;
            this.$toast(res.msg);
            
            this.withdrawData();
          } else {
            this.$dialog.alert({
              message: res.msg
            });
          }
        });
      }
    },
    editAccount(){
      let json = {
        id_card_valid_type:this.userForm.id_card_valid_type,
        id_card_valid_start_time:this.userForm.id_card_valid_start_time,
        id_card_valid_end_time:this.userForm.id_card_valid_end_time,
        mobile:this.userForm.mobile,
      }
      $http.post("plugin.integration-pay-share.frontend.withdraw.edit-account", {form:json}, "加载中..").then(res => {
        if (res.result == 1) {
          let data = res.data;
          this.$toast(res.msg);
          this.withdrawData();
        } else {
          this.$dialog.alert({
            message: res.msg
          });
        }
      });
    },
    withdrawData() {
      $http.get("plugin.integration-pay-share.frontend.withdraw.index",{}, "加载中..").then(res => {
        if (res.result == 1) {
          let data = res.data; //没有数据返回的是一个空数组
          if(!Array.isArray(data)) {
            this.withdrawInfo = data;
            this.userForm.mobile = data.mobile;
            this.userForm.name = data.name;
            this.userForm.id_card_no = data.id_card_no;
            this.userForm.id_card_type = data.id_card_type;
            this.userForm.id_card_valid_type = data.id_card_valid_type;
            this.userForm.id_card_valid_start_time = data.id_card_valid_start_time;
            this.userForm.id_card_valid_end_time = data.id_card_valid_end_time;
            this.cardValidName = data.id_card_valid_type_name;
            this.cardTypeName = data.id_card_type_name;
            this.startTime = (data.id_card_valid_start_time_stamp * 1000);
            this.endTime = (data.id_card_valid_end_time_stamp * 1000);
          }
          if(this.withdrawInfo.id) {
            this.btnStatus = 1;
          }
          
        } else {
          this.$dialog.alert({
            message: res.msg
          });
        }
      });
    },
    dateCancel() {
      this.dateShow = false;
    },
    dateConfirm(event) {
      if(this.cardTimeType == 'start') {
        this.userForm.id_card_valid_start_time = this.timestampToTime(Date.parse(event));
        this.startTime = Date.parse(event);
      } else {
        this.userForm.id_card_valid_end_time = this.timestampToTime(Date.parse(event));
        this.endTime = Date.parse(event);
      }
      this.dateShow = false;
    },
    //将时间戳转换成正常时间格式
    timestampToTime: function(timestamp, key) {
      if (!Number(timestamp) || timestamp == "") {
        return "";
      }
      let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear() + "-";
      let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
      let D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      return Y + M + D;
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
  
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    // this.initData();
    
    this.withdrawData();
  }, //如果页面有keep-alive缓存功能，这个函数会触发
  }